package com.itheima.web;

import com.alibaba.fastjson.JSON;
import com.itheima.dao.mapper.UserMapper;
import com.itheima.pojo.*;
import com.itheima.service.BookService;
import com.itheima.service.UserService;
import com.itheima.service.impl.BookServiceImpl;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.web.base.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    UserService service = new UserServiceImpl();
    BookService bs = new BookServiceImpl();

    /**
     * 管理员增加用户
     * @param request
     * @param response
     * @throws Exception
     */
    public void addUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String s = reader.readLine();
        System.out.println(s);

        User user = JSON.parseObject(s, User.class);
        user.setCard("book"+user.getUsername());
        System.out.println(user);

        boolean flag = service.addUser(user);

        if (flag){
            System.out.println(user);
            response.getWriter().write("success");
        }else {
            user = null;
            response.getWriter().write("lose");
        }

    }

    /**
     * 批量删除用户
     * @param request
     * @param response
     * @throws Exception
     */
    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        //转为数组
        int[]  ids = JSON.parseObject(params, int[].class);

        service.deleteByIds(ids);

        response.getWriter().write("success");
    }

    /**
     * 分页查询
     * @param request
     * @param response
     * @throws Exception
     */
    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        PageBean<User> pageBean = service.selectByPage(currentPage, pageSize);

        String s = JSON.toJSONString(pageBean);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 分页条件查询
     * @param request
     * @param response
     * @throws Exception
     */
    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        //转为book对象
        User user = JSON.parseObject(params, User.class);

        PageBean<User> pageBean = service.selectByPageAndCondition(currentPage, pageSize, user);

        String s = JSON.toJSONString(pageBean);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(s);
    }

    /**
     * 删除
     * @param request
     * @param response
     * @throws Exception
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        System.out.println("---------"+params+"---------");

        //把id转为整型
        Integer id = JSON.parseObject(params, Integer.class);

        User user = service.selectById(id);
        String username = user.getUsername();

        int i = bs.deleteUser(username);
        int j = bs.deleteHistory(username);

        if (i>0&&j>0){
            service.delete(id);
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }

    }

    /**
     * 接收编辑数据传过来的id
     * @param request
     * @param response
     * @throws Exception
     */
    public void acceptId(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        Integer acceptUserId = JSON.parseObject(params, Integer.class);

        HttpSession session = request.getSession();
        session.setAttribute("acceptUserId",acceptUserId);

        response.getWriter().write("success");

    }

    /***
     * 编辑用户
     * @param request
     * @param response
     * @throws Exception
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        HttpSession session = request.getSession();
        Integer  acceptUserId = (Integer)session.getAttribute("acceptUserId");

        User user = JSON.parseObject(params, User.class);


        user.setId(acceptUserId);
        System.out.println(user);

        int i = service.update(user);
        System.out.println(i);
        if (i>0){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }

    }

    /**
     * 用户自行修改密码
     * @param request
     * @param response
     * @throws Exception
     */
    public void repassword(HttpServletRequest request, HttpServletResponse response) throws Exception{
        BufferedReader reader = request.getReader();
        String params = reader.readLine();

        User user = JSON.parseObject(params, User.class);
        System.out.println(user);

        int i = service.update(user);
        if (i>0){
            response.getWriter().write("success");
        }else {
            response.getWriter().write("lose");
        }
    }

    /**
     * 查看借书历史记录
     * @param request
     * @param response
     * @throws Exception
     */
    public void checkmy(HttpServletRequest request, HttpServletResponse response) throws Exception{
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String username = user.getUsername();

        List<History> histories = service.selectHistory(username);

        String s = JSON.toJSONString(histories);

        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write(s);
    }
}
